package cn.liontalk.demo;

/**
 * @author: 周哲
 * @package: cn.liontalk.demo
 * @description: 同时访问静态synchronized 和非静态synchronized的方法
 * @date: 2019/1/26 15:57
 * @version: V1.0
 */
public class SynchronizedStaticYesAndNo8 implements Runnable {

    static SynchronizedStaticYesAndNo8 instance = new SynchronizedStaticYesAndNo8();

    public SynchronizedStaticYesAndNo8() {
        super();
        System.out.println("This ==========> " + this);
    }

    public void run() {
        if (Thread.currentThread().getName().equals("Thread-0")) {
            method();
        } else {
            method2();
        }

       /* method();
        method2();*/
    }


    public synchronized void method() {
        System.out.println("加锁的方法1：" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("加锁方法完成。。。" + Thread.currentThread().getName());


    }

    public synchronized static void method2() {
        System.out.println("加锁的方法2：" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("加锁方法完成。。。" + Thread.currentThread().getName());

    }

    public static void main(String[] args) {
        Thread thread = new Thread(instance);
        Thread thread2 = new Thread(instance);
        thread.start();
        thread2.start();
        while (thread.isAlive() || thread.isAlive()) {

        }
        System.out.println("Finished .....");
    }



}
